package com.fss.experience.dao;
import com.fssDataModels.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class ExperienceDaoImpl implements ExperienceDao{
	private SessionFactory _sessions;
	
	public ExperienceDaoImpl()
	{
		// Create the SessionFactory from hibernate.cfg.xml
		_sessions = new Configuration()				
				.configure()
				.buildSessionFactory();
	}
	//CRUD functions
	public TbExperience getExperience(Integer experienceId)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		TbExperience result = null;
		try {
			tx = session.beginTransaction();
			String hql=	"select experience " +
						"from TbExperience as experience " +
						"where experience.experienceId=:experienceId ";		
			
			Query query = session.createQuery(hql);
			query.setParameter("experienceId", experienceId);
			
			if(!query.list().isEmpty())				
				result=(TbExperience)query.list().get(0);
			
			tx.commit();					
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			//do nothing else
		}
		finally {
			session.close();
		}
		return result;
	}
	//return: - 0 --> failed
	//		  - 1 --> success
	public int insertExperience(TbExperience experience)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		int result=1;
		try {
			tx = session.beginTransaction();
			session.persist(experience);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		return 1;
	}
	//return: - 0 --> failed
	//		  - 1 --> success
	public int updateExperience(TbExperience experience)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		int result=1;
		try {
			tx = session.beginTransaction();
			session.update(experience);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		
		
		return result;
	}
	
	//function: deleteActivity
	//return: - 0 --> not deleted
	//		  - 1 --> deleted
	public int deleteExperience(TbExperience experience)
	{
		int success=0;
		Session session = _sessions.openSession();
		Integer experienceId= experience.getExperienceId();
		
		try {		    
		      String hql = "delete from TbExperience experience where experience.experienceId = :experienceId";
		      Query query = session.createQuery(hql);
		      query.setParameter("experienceId", experienceId);
		      int row = query.executeUpdate();
		      if (row == 0){
		        success=0;//delete command not successfully executed
		      }
		      else{
		        success=1;//delete command successfully executed
		      }
		    }
		    catch(HibernateException he){
		    	//do nothing
		    	success=0;//delete command not successfully executed
		    }
		    finally {
				session.close();
			}
		return success;
	}
	
	//comments related functions
	public List getComments(TbExperience experience)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		List result = null;
		String hql;
		int experienceId = experience.getExperienceId();
		try {
			tx = session.beginTransaction();						
				hql=	"select comment "+
							"from TbComment comment " +
							"where comment.tbExperience.experienceId = :experienceId "+
							"order by comment.createdTime desc ";//get the most recently uploaded activities
			
			Query query = session.createQuery(hql);
			query.setParameter("experienceId", experienceId);			
			
			
			if(!query.list().isEmpty())				
				result=query.list();
			
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			//do nothing else
		}
		finally {
			session.close();
		}
		
		
		return result;
	}
	
	//function: adComment
	//	when performing the addition of the comment to the comment list of an experience, a new 
	//  comment id of the form <experienceId_commentNumber> is created
	public int addComment(TbExperience experience,TbComment comment)
	{
		
		experience = this.getExperience(experience.getExperienceId());
		//create the Id of the new comment
		int commentNumber= experience.getCommentCounter();
		comment.setCommentId(experience.getExperienceId()+"_"+commentNumber);//the commentId is of the
														//form <experienceId_commentNumber>
		comment.setTbExperience(experience);			
		
		Session session = _sessions.openSession();
		Transaction tx = null;
		int result=1;
		try {
			tx = session.beginTransaction();
			session.persist(comment);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		return result;
		
	}
	public int deleteComment( TbComment comment)
	{
		int success=0;
		Session session = _sessions.openSession();		
		String commentId = comment.getCommentId();
		
		try {		    
		      String hql = "delete from TbComment comment where comment.commentId = :commentId";
		      Query query = session.createQuery(hql);
		      query.setParameter("commentId", commentId);
		      int row = query.executeUpdate();
		      if (row == 0){
		        success=0;//delete command not successfully executed
		      }
		      else{
		        success=1;//delete command successfully executed		        
		      }
		    }
		    catch(HibernateException he){
		    	//do nothing
		    	success=0;//delete command not successfully executed
		    }
		    finally {
				session.close();
			}
		return success;
	}
	
	public int insertExperienceType(TbExperienceType type)
	{
		Session session = _sessions.openSession();
		int result=1;
		
		Transaction tx = null;
		
		try {
			tx = session.beginTransaction();
			session.persist(type);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		return result;
	}
	
	public TbExperienceType getExperienceType(int typeId)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		TbExperienceType result = null;
		try {
			tx = session.beginTransaction();
			String hql=	"select type " +
						"from TbExperienceType as type " +
						"where type.typeId =:typeId ";		
			
			Query query = session.createQuery(hql);
			query.setParameter("typeId", typeId);
			
			if(!query.list().isEmpty())				
				result=(TbExperienceType)query.list().get(0);
			
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			//do nothing else
		}
		finally {
			session.close();
		}
		return result;
	}
	
	public int deleteExperienceType(int typeId)
	{

		int success=0;
		Session session = _sessions.openSession();
		
		try {		    
		      String hql = "delete from TbExperienceType experienceType where experienceType.typeId = :typeId";
		      Query query = session.createQuery(hql);
		      query.setParameter("typeId", typeId);
		      int row = query.executeUpdate();
		      if (row == 0){
		        success=0;//delete command not successfully executed
		      }
		      else{
		        success=1;//delete command successfully executed
		      }
		    }
		    catch(HibernateException he){
		    	//do nothing
		    	success=0;//delete command not successfully executed
		    }
		    finally {
				session.close();
			}
		return success;
	}
}
